//related to header2.ui
var root = ui("$");// 或者var root = ui("root");

var app = sm('do_App');
var page = sm("do_Page");
var do_External = sm("do_External");
var calculation = 0;
var nf = sm("do_Notification");
//返回按钮
var back_btn = ui("back_btn");
//结账跳转
var pay = ui("pay");
var payType = 0;//0 微信 1支付宝 2余额 3 券

ui("input").text =calculation;
init();


page.on("message",function(data){

	if(data == "AC"){
		calculation = 0;
		ui("input").text =0;
	}
	else if(data == "+"||data == "-"||data == "*"||data == "/"){
		if(calculation.charAt(calculation.length - 1)=="+"||calculation.charAt(calculation.length - 1)=="-"||
				calculation.charAt(calculation.length - 1)=="*"||calculation.charAt(calculation.length - 1)=="/"){	
		}else{
			calculation +=data;
			ui("input").text =calculation;
		}
	}else if (data =="="){
		calculation = eval(calculation);
		ui("input").text =calculation;
	}else{
		if(ui("input").text == 0){
			ui("input").text = "";
			calculation +=data;
			ui("input").text =calculation;
		}else{
			calculation +=data;
			ui("input").text =calculation;
		}
	}
	

});

//绑定消息
function init() {
	ui("plus").on("touch",function(){
		page.fire("message","+");
	});
	ui("minus").on("touch",function(){
		page.fire("message","-");
	});
	ui("times").on("touch",function(){
		page.fire("message","*");
	});
	ui("divide").on("touch",function(){
		page.fire("message","/");
	});
	ui("is").on("touch",function(){
		page.fire("message","=");
	});
	ui("AC").on("touch",function(){
		page.fire("message","AC");
	});
	ui("num0").on("touch",function(){
		page.fire("message","0");
	});
	ui("num1").on("touch",function(){
		page.fire("message","1");
	});
	ui("num2").on("touch",function(){
		page.fire("message","2");
	});
	ui("num3").on("touch",function(){
		page.fire("message","3");
	});
	ui("num4").on("touch",function(){
		page.fire("message","4");
	});
	ui("num5").on("touch",function(){
		page.fire("message","5");
	});
	ui("num6").on("touch",function(){
		page.fire("message","6");
	});
	ui("num7").on("touch",function(){
		page.fire("message","7");
	});
	ui("num8").on("touch",function(){
		page.fire("message","8");
	});
	ui("num9").on("touch",function(){
		page.fire("message","9");
	});
}

ui("WeChat").on("touch",function(){
	payType = 0;
	ui("WeChat").bgColor = "F5A623FF";
	ui("Alipay").bgColor = "5A72D7FF";
	ui("money").bgColor = "5A72D7FF";
	ui("consumption").bgColor = "5A72D7FF";
	
});
ui("Alipay").on("touch",function(){
	payType = 1;
	ui("WeChat").bgColor = "5A72D7FF";
	ui("Alipay").bgColor = "F5A623FF";
	ui("money").bgColor = "5A72D7FF";
	ui("consumption").bgColor = "5A72D7FF";
	
});
ui("money").on("touch",function(){
	payType = 2;
	ui("WeChat").bgColor = "5A72D7FF";
	ui("Alipay").bgColor = "5A72D7FF";
	ui("money").bgColor = "F5A623FF";
	ui("consumption").bgColor = "5A72D7FF";
	
});
ui("consumption").on("touch",function(){
	payType = 3;
	ui("WeChat").bgColor = "5A72D7FF";
	ui("Alipay").bgColor = "5A72D7FF";
	ui("money").bgColor = "5A72D7FF";
	ui("consumption").bgColor = "F5A623FF";
	
});



pay.on("touch",function(){
	var mData = {"payType":payType,"money":calculation};
	app.openPage({
		source:"source://view/Consumption/SweepCodePayment.ui", 
		data:mData
		});
});




/*****返回按钮*****/
back_btn.on("touch",function(){
	app.closePage();
})


//物理键返回事件
page.on("back", function(){
	app.closePage();
});